package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.BookDao;
import com.oocl.pojo.Book;
import com.oocl.util.DBUtil;

public class BookDaoImpl implements BookDao {

	public int addBook(Book b) {
		String sql = "INSERT INTO Books VALUES(seq01.nextval,?,?,?,?,?)";
		PreparedStatement pst = null;
		Connection conn = null;
		int result = 0;
		try {
			conn = DBUtil.createConnection();
			pst = conn.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
			pst.setDouble(3, b.getPrice());
			pst.setBoolean(4, b.isNew());
			pst.setString(5, b.getPublisher());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, null);
		}
		return result;
	}

	public int deleteBook(String name) {
		String sql = "delete from Books where name=?";
		PreparedStatement pst = null;
		Connection conn = null;
		int result = 0;
		try {
			conn = DBUtil.createConnection();
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			result = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, null);
		}
		return result;
	}

	public int updateBook(Book b) {
		String sql = "update Books set name=?,publishDate=?,price=?,isNew=?,publisher=? where id=?";
		PreparedStatement pst = null;
		Connection conn = null;
		int result = 0;
		try {
			conn = DBUtil.createConnection();
			pst = conn.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
			pst.setDouble(3, b.getPrice());
			pst.setBoolean(4, b.isNew());
			pst.setString(5, b.getPublisher());
			pst.setInt(6, b.getId());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, null);
		}
		return result;
	}

	public Book loadBook(Integer id) {
		String sql = "select * from Books where id=?";
		PreparedStatement pst = null;
		Connection conn = null;
		ResultSet rSet = null;
		try {
			conn = DBUtil.createConnection();
			pst = conn.prepareStatement(sql);
			pst.setInt(1, id);
			rSet = pst.executeQuery();
			if (rSet.next()) {
				Book b = new Book();
				b.setName(rSet.getString("name"));
				b.setNew(rSet.getBoolean("isNew"));
				b.setPrice(rSet.getDouble("price"));
				b.setPublishDate(rSet.getDate("publishDate"));
				b.setPublisher(rSet.getString("publisher"));
				return b;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, rSet);
		}
		return null;
	}

	public List<Book> findAll() {
		List<Book> list = new ArrayList<Book>();
		String sql = "select * from Books";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			if (rs.next()) {
				Book b = new Book();
				b.setId(rs.getInt("id"));
				b.setName(rs.getString("name"));
				b.setNew(rs.getBoolean("isNew"));
				b.setPrice(rs.getDouble("price"));
				b.setPublishDate(rs.getDate("publishDate"));
				b.setPublisher(rs.getString("publisher"));
				list.add(b);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, rs);
		}
		return list;
	}

	public int batchAddBook(List<Book> list) {

		String sql = "INSERT INTO Books VALUES(seq01.nextval,?,?,?,?,?)";
		PreparedStatement pst = null;
		Connection conn = null;
		int result = 0;
		int count=0;
		try {
			conn = DBUtil.createConnection();
			conn.setAutoCommit(false);
			pst = conn.prepareStatement(sql);
			for (Book b : list) {
				pst.setString(1, b.getName());
				pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
				pst.setDouble(3, b.getPrice());
				pst.setBoolean(4, b.isNew());
				pst.setString(5, b.getPublisher());
				pst.addBatch();
				if(++count%100==0){
					System.out.println("transaction commit");
					conn.commit();
				}
			}
			result = pst.executeBatch().length;
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, null);
		}
		return result;
	}

}
